﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Collections;
using Aplikacja_PostgreSQL;
using System.Windows.Forms;

namespace test2
{
    class Files
    {
        private static StreamReader str;
        private static StreamWriter sw;
        private static ArrayList arrList;
        public static void OpenFile()
        {
            try
            {
                str = new StreamReader("temp.data");
                string tmp = "";
                arrList = new ArrayList();
                while (tmp != null)
                {
                    tmp = str.ReadLine();
                    if (tmp != null)
                        arrList.Add(tmp);
                }
                str.Close();
                MainWindow.DbSerwer = arrList[0].ToString();
                MainWindow.DbDatabase = arrList[1].ToString();
                MainWindow.DbUserID = arrList[2].ToString();
                MainWindow.DbPassword = arrList[3].ToString();
                MainWindow.DbPort = arrList[4].ToString();
            }
            catch (FileNotFoundException exp)
            {
                CreateFile();
            }
            catch (Exception e)
            {
                MainWindow.DbSerwer = "127.0.0.1";
                MainWindow.DbDatabase = "testowa";
                MainWindow.DbUserID = "test";
                MainWindow.DbPassword = "abc";
                MainWindow.DbPort = "5432";
            }
        }

        public static void ReWriteFile()
        {
            try
            {
                sw = new StreamWriter("temp.data");
                sw.WriteLine(MainWindow.DbSerwer);
                sw.WriteLine(MainWindow.DbDatabase);
                sw.WriteLine(MainWindow.DbUserID);
                sw.WriteLine(MainWindow.DbPassword);
                sw.WriteLine(MainWindow.DbPort);
                sw.Flush();
                sw.Close();
                OpenFile();
            }
            catch (System.Security.SecurityException exp)
            {
                MessageBox.Show(exp.Message.ToString());
            }
        }

        public static void CreateFile()
        {
            try
            {
                sw = new StreamWriter("temp.data");
                string dbSerwer1 = "127.0.0.1";
                string dbDatabase1 = "testowa";
                string dbUserID1 = "test";
                string dbPassword1 = "abc";
                string dbPort1 = "5432";

                sw.WriteLine(dbSerwer1);
                sw.WriteLine(dbDatabase1);
                sw.WriteLine(dbUserID1);
                sw.WriteLine(dbPassword1);
                sw.WriteLine(dbPort1);
                sw.Flush();
                sw.Close();
                OpenFile();
            }
            catch (System.Security.SecurityException exp)
            {
                MessageBox.Show(exp.Message.ToString());
            }
        }
    }
}
